import axios from "axios";
import { ElMessage } from "element-plus";

const request = axios.create({
  baseURL: "http://localhost:8005/",
  timeout: 1000,
});

request.defaults.withCredentials = true; // 配置为true 表示发送请求的时候携带请求凭证cookie

// 请求拦截器
request.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    console.log(error);
    return Promise.reject(error);
  }
);

// 响应拦截器
request.interceptors.response.use(
  (response) => {
    if (
      response.data.code != 200 &&
      ![40000, 40100].includes(response.data.code)
    ) {
      console.log(response);
      ElMessage.error("响应错误");
      return response;
    }
    return response;
  },
  (error) => {
    console.log(error);
    return Promise.reject(error);
  }
);

export default request;
